package router

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	"os"
	"test_singo/app/demo"
	"test_singo/app/user"
	"test_singo/middleware"
)

type Option func(*gin.Engine)

var options []Option

// 注册app的路由配置
func Include(opts ...Option) {
	options = append(options, opts...)
}

func NewRouter() *gin.Engine {
	// 将子路由include进来
	Include(
		user.Routers,
		demo.Routers,
	)

	gin.ForceConsoleColor()

	r := gin.Default()

	// 注册验证器
	//if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
	//	v.RegisterValidation("bookabledate", demo.BookableDate)
	//}
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		v.RegisterValidation("bookabledate", demo.BookableDate)
	}

	// 中间件
	r.Use(middleware.Session(os.Getenv("SESSION_SECRET")))
	r.Use(middleware.Cors())
	r.Use(middleware.CurrentUser())

	// 路由
	for _, opt := range options {
		opt(r)
	}

	return r
}
